﻿Public Class frmMaterialDetail
	Private m_CurCategoryID As String = "" ' Lưu loại nguyên liệu

  Public Function AddMaterial() As Boolean
    Try
      Dim dbAccess As DataBaseAccess = New DataBaseAccess
      Dim SQLString As String = "SELECT * FROM tbl_materialdetail"
      Dim dtTable = dbAccess.GetDataTable(SQLString)
      Dim dtRow = dtTable.NewRow
      SetMaterialData(dtRow)
      dtTable.Rows.Add(dtRow)
      Return dbAccess.UpdateDataTable(SQLString, dtTable)
    Catch ex As Exception
      Throw New Exception(" Can not insert material record ")
      Return False
    End Try
  End Function

  Private Sub SetMaterialData(ByRef drow As DataRow)
    drow("Name") = txtName.Text.Trim()
    drow("CategoryID") = m_CurCategoryID
    drow("CreateDate") = dtpCreateDate.Value.Year.ToString()
    drow("CreateMonth") = dtpCreateDate.Value.Month.ToString()
    drow("CreateYear") = dtpCreateDate.Value.Day.ToString()
    drow("EndDate") = dtpEndDate.Value.Year.ToString()
    drow("EndMonth") = dtpEndDate.Value.Month.ToString()
    drow("EndYear") = dtpEndDate.Value.Day.ToString()
    drow("ImportPrice") = Convert.ToDouble(IIf(String.IsNullOrEmpty(txtPrice.Text.Trim()), 0, txtPrice.Text.Trim()))
    drow("Unit") = cmbUnit.Text.Trim()
    drow("Quality") = txtQuality.Text.Trim()
    drow("MaterialID") = txtMaterialID.Text.Trim()
    drow("Comments") = txtComments.Text.Trim()
  End Sub

  Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
		If txtCategory.Text.Trim().Equals("") Then
			MessageBox.Show("Bạn phải chọn một loại nguyên liệu.", "Loại Nguyên Liệu", MessageBoxButtons.OK, MessageBoxIcon.Error)
			txtCategory.Focus()
		End If
    Me.DialogResult = Windows.Forms.DialogResult.OK
  End Sub

	Private Sub btnAddCategory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddCategory.Click
		Dim fMaterialCategory As frmMaterialCategoryManage = New frmMaterialCategoryManage()
		fMaterialCategory.ShowDialog(Me)
		If (fMaterialCategory.DialogResult = Windows.Forms.DialogResult.OK) Then
		End If
	End Sub

	Private Sub frmMaterialDetail_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		cmbUnit.SelectedIndex = 0
	End Sub
End Class